package com.nifostasky.dlx.producer;

import com.nifostasky.dlx.config.RabbitTtlDlxConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 死信交换机
 */
@Component
public class DelayProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMsg() {
        MessageProperties mp = new MessageProperties();
        String msg = "这是一条有生命周期的消息，发送时间为：" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Message message = new Message(msg.getBytes(),mp);
        rabbitTemplate.convertAndSend(RabbitTtlDlxConfig.EXCHANGE_NAME_TTL, RabbitTtlDlxConfig.ROUTING_KEY_TTL, message);
    }
}
